# Create your views here.
from django.http import HttpResponse
from PaSt.past.models import Pandoleros
from PaSt.past.models import Turns
from PaSt.past.models import parseDates
from PaSt.past.team import Team
import style
import os
import datetime


#host="localhost:8000"
host="193.2.60.75:8080"
ENEMY_ID=40




#def general(request, id, sType=2,daterange="1970-01-01_00:00:00;2020-01-01_00:00:00"):
def general(request, id, sType=2,daterange="1970-01-01_00:00:00;2020-01-01_00:00:00"):
	from django.db import models

	#INICIALIZACIJA
	sTypeDesc = ["tekma","trening", None]
	par = sTypeDesc[int(sType)]

	p = Pandoleros.objects.filter(idP = id)[0]
	p.getTurns(par,daterange)

#IML/ZACETEK
	response=HttpResponse("")

	response.write("<head>")
	response.write(style.getStyle())
	response.write("<link href=\"screen\" rel=\"stylesheet\" type=\"text/css\"></link>")
	response.write("</head>")
	response.write("<p class=\"panName\">"+p.name+"</p>")

	#Podnaslov
	if sType == "2":
		sTypeDesc2="Splosno"
	else:
		sTypeDesc2 = par	
	response.write("<p id=\"stype\">("+str(sTypeDesc2)+")</p>")
	response.write("<body bgcolor=\"bfbfbf\"")


	#Stevilo tock & fuori campov
	response.write("<table border=\"0\">")
	response.write("<tr><td><p class=\"section\">Napad</p></td></tr>")
	response.write("<tr><td><p class=\"graphTitle\">Stevilo tock & fuori campov</p>")
	response.write("<p class=\"stats\">Skupno stevilo tock: <b>"+str(p.scoreTotal())+"</b></p>")
	response.write("<p class=\"stats\">Skupno stevilo FC: <b>"+str(p.fcTotal())+"</b></p>")
	response.write("<p class=\"stats\">Povprecno stevilo tock na turn: <b>"+str(round(p.scoreAverage(),2))+"</b></p>")
	response.write("<p class=\"stats\">Povprecno stevilo FC na turn: <b>"+str(round(p.fcAverage(),2))+"</b></p>")
	response.write("</td></tr>")
	response.write("<tr><td><img width=\"700\" src=\"http://"+host+"/past/graph/"+id+"/0/"+str(sType)+"/"+daterange+"\"></img></td>")
	response.write("</tr></table>")
	
	#Trenutna forma
	response.write("<table border=\"0\">")
	response.write("<tr><td><p class=\"graphTitle\">Trenutna forma</p></td></tr>")
	#response.write("<p class=\"stats\">Skupno stevilo tock: <b>"+str(p.scoreTotal())+"</b></p>")
	#response.write("<p class=\"stats\">Skupno stevilo FC: <b>"+str(p.fcTotal())+"</b></p>")
	#response.write("<p class=\"stats\">Povprecno stevilo tock na turn: <b>"+str(round(p.scoreAverage(),2))+"</b></p>")
	#response.write("<p class=\"stats\">Povprecno stevilo FC na turn: <b>"+str(round(p.fcAverage(),2))+"</b></p>")
	#response.write("</td></tr>")
	#response.write("<tr><td><img width=\"700\" src=\"http://"+host+"/past/graph/"+id+"/13/"+str(sType)+"\"></img></td>")
	response.write("<tr><td><img width=\"700\" src=\"http://"+host+"/past/graph/"+id+"/13/"+str(sType)+"/"+daterange+"\"></img></td>")
	response.write("</tr></table>")


	#Serve&udarci (serve)
	servVector = p.servDistributionVector();
	unServVector=p.servReasonVector();
	okServ=p.servSucess();
	tServ=p.servTurn()
	response.write("<p class=\"graphTitle\">Serve&udarci</p>")
	response.write("<table border=\"0\">")
	response.write("<tr>");
	response.write("<td>");
	#response.write("<img width=\"350\" src=\"http://"+host+"/past/graph/"+id+"/9/"+str(sType)+"\"></img>")
	response.write("<img width=\"350\" src=\"http://"+host+"/past/graph/"+id+"/9/"+str(sType)+"/"+daterange+"\"></img></td>")
	response.write("</td>");
	response.write("<td>");
	#response.write("<img width=\"350\" src=\"http://"+host+"/past/graph/"+id+"/10/"+str(sType)+"\"></img>")
	response.write("<img width=\"350\" src=\"http://"+host+"/past/graph/"+id+"/10/"+str(sType)+"/"+daterange+"\"></img></td>")
	response.write("</td>");
	response.write("</tr>");
	response.write("<td>");	
	response.write("<p class=\"stats\"><h3>Uspelih serv: "+str(round(okServ*100,2))+"%</h3></p>")
	response.write("<p class=\"stats\"><h4>Turnov, ko je uspel servirati: "+str(round(tServ*100,2))+"%</h4></p>")
	response.write("<p class=\"stats\">Uspele serve (1): <b>"+str(round(servVector[0]*100,2))+" %</b></p>")
	response.write("<p class=\"stats\">Uspele serve (2): <b>"+str(round(servVector[1]*100,2))+" %</b></p>")
	response.write("<p class=\"stats\">Uspele serve (3): <b>"+str(round(servVector[2]*100,2))+" %</b></p>")
	response.write("</td>")
	response.write("<td>")	
	response.write("<p class=\"stats\"><h3>Razlog za neuspesne serve</h3></p>")
	response.write("<p class=\"stats\"><h4>_</h4></p>")
	response.write("<p class=\"stats\">Zgresen pandolo: <b>"+str(round(unServVector[1]*100,2))+" %</b></p>")
	response.write("<p class=\"stats\">Out: <b>"+str(round(unServVector[0]*100,2))+" %</b></p>")
	response.write("<p class=\"stats\">Ujet pandolo: <b>"+str(round(unServVector[2]*100,2))+" %</b></p>")
	response.write("</td>");
	response.write("</tr>");
	response.write("<tr>");
	response.write("<td>");
	#response.write("<img width=\"350\" src=\"http://"+host+"/past/graph/"+id+"/11/"+str(sType)+"\"></img>")
	response.write("<img width=\"350\" src=\"http://"+host+"/past/graph/"+id+"/11/"+str(sType)+"/"+daterange+"\"></img>")
	response.write("</td>");
	response.write("<td>");
	#response.write("<img width=\"350\" src=\"http://"+host+"/past/graph/"+id+"/12/"+str(sType)+"\"></img>")
	response.write("<img width=\"350\" src=\"http://"+host+"/past/graph/"+id+"/12/"+str(sType)+"/"+daterange+"\"></img>")
	response.write("</td>");
	response.write("<tr>");
	
	
	#Serve&udarci (udarci)
	response.write("<tr>");
	tH = p.hitPositive()
	tHit = p.hitEventVector()
	tUnHit = p.hitReasonVector()
	response.write("<td>");	
	#response.write("<p class=\"stats\"><h3>Uspelih serv: "+str(round(okServ*100,2))+"%</h3></p>")
	response.write("<p class=\"stats\"><h4>Turnov, ko je naredil tocke: "+str(round(tH*100,2))+"%</h4></p>")
	response.write("<p class=\"stats\">Fuoricampo: <b>"+str(round(tHit[0]*100,2))+" %</b></p>")
	response.write("<p class=\"stats\">+12 mazz -Fuoricampo: <b>"+str(round(tHit[1]*100,2))+" %</b></p>")
	response.write("<p class=\"stats\">-12 mazz: <b>"+str(round(tHit[2]*100,2))+" %</b></p>")
	response.write("<p class=\"stats\">Pandolo se ni dvignil: <b>"+str(round(tHit[3]*100,2))+" %</b></p>")
	response.write("<p class=\"stats\">Pandolo zgresen v zraku: <b>"+str(round(tHit[4]*100,2))+" %</b></p>")
	response.write("<p class=\"stats\">Pandolo ujet: <b>"+str(round(tHit[5]*100,2))+" %</b></p>")
	response.write("</td>")
	response.write("<td>")	
	response.write("<p class=\"stats\"><h3>Razlog za neuspesne udarce</h3></p>")
	response.write("<p class=\"stats\"><h4>_</h4></p>")
	response.write("<p class=\"stats\"><h4>_</h4></p>")
	response.write("<p class=\"stats\">Pandolo se ni dvignil: <b>"+str(round(tUnHit[0]*100,2))+" %</b></p>")
	response.write("<p class=\"stats\">Pandolo zgresen v zraku: <b>"+str(round(tUnHit[1]*100,2))+" %</b></p>")
	response.write("<p class=\"stats\">Pandolo ujet: <b>"+str(round(tUnHit[2]*100,2))+" %</b></p>")
	response.write("</td>");
	response.write("</tr>");
	response.write("</tr>");
	response.write("</table>");


	#Korelacija doping<->tocke
	response.write("<p class=\"graphTitle\">Korelacija doping<->tocke</p>")
	response.write("<table border=\"0\">")
	response.write("<tr>")
	response.write("<td>")
	response.write("<p class=\"stats\">K. Koeficient: <b>"+str(p.dopingCC(g='joola'))+"</b></p>")
	#response.write("<img width=\"350\" src=\"http://"+host+"/past/graph/"+id+"/2/"+str(sType)+"\"></img>")
	response.write("<img width=\"350\" src=\"http://"+host+"/past/graph/"+id+"/2/"+str(sType)+"/"+daterange+"\"></img>")
	response.write("</td>")
	response.write("<td>")
	response.write("<p class=\"stats\">K. Koeficient: <b>"+str(p.dopingCC(g='spric'))+"</b></p>")
	#response.write("<img width=\"350\" src=\"http://"+host+"/past/graph/"+id+"/3/"+str(sType)+"\"></img>")
	response.write("<img width=\"350\" src=\"http://"+host+"/past/graph/"+id+"/3/"+str(sType)+"/"+daterange+"\"></img>")
	response.write("</td>")
	response.write("</tr>")
	response.write("<tr>")
	response.write("<p class=\"stats\">K. Koeficient: <b>"+str(p.dopingCC(g='both'))+"</b></p>")
	#response.write("<img width=\"700\" src=\"http://"+host+"/past/graph/"+id+"/4/"+str(sType)+"\"></img>")
	response.write("<img width=\"700\" src=\"http://"+host+"/past/graph/"+id+"/4/"+str(sType)+"/"+daterange+"\"></img>")
	response.write("</tr></table>")

	#Obramba
	response.write("<p class=\"section\">Obramba</p>")
	response.write("<table>")
	response.write("<tr>")
	response.write("<td>")
	response.write("<p class=\"stats\">Ulovljeni pandoli: <b>"+str(p.caughtPanCountOthers())+"</b></p>")
	response.write("<p>Pandole so mu ulovili:</p>")
	#response.write("<img width=\"350\" src=\"http://"+host+"/past/graph/"+id+"/5/"+str(sType)+"\"></img>")
	response.write("<img width=\"350\" src=\"http://"+host+"/past/graph/"+id+"/5/"+str(sType)+"/"+daterange+"\"></img>")
	response.write("</td>")
	response.write("<td>")
	response.write("<p class=\"stats\">Ulovljeni pandoli: <b>"+str(p.caughtPanCount())+"</b></p>")
	response.write("<p>Pandolo je ulovil:</p>")
	#response.write("<img width=\"350\" src=\"http://"+host+"/past/graph/"+id+"/6/"+str(sType)+"\"></img>")
	response.write("<img width=\"350\" src=\"http://"+host+"/past/graph/"+id+"/6/"+str(sType)+"/"+daterange+"\"></img>")
	response.write("</td>")
	response.write("</tr>")
	response.write("<tr>")
	response.write("<td>")
	response.write("<p class=\"stats\">Izbite baze: <b>"+str(p.baseHitCountOthers())+"</b></p>")
	response.write("<p>Bazo so mu izbili:</p>")
	#response.write("<img width=\"350\" src=\"http://"+host+"/past/graph/"+id+"/7/"+str(sType)+"\"></img>")
	response.write("<img width=\"350\" src=\"http://"+host+"/past/graph/"+id+"/7/"+str(sType)+"/"+daterange+"\"></img>")
	response.write("</td>")
	response.write("<td>")
	response.write("<p class=\"stats\">Izbite baze: <b>"+str(p.baseHitCount())+"</b></p>")
	response.write("<p>Bazo je izbil:</p>")
	#response.write("<img width=\"350\" src=\"http://"+host+"/past/graph/"+id+"/8/"+str(sType)+"\"></img>")
	response.write("<img width=\"350\" src=\"http://"+host+"/past/graph/"+id+"/8/"+str(sType)+"/"+daterange+"\"></img>")
	response.write("</td>")
	response.write("</tr>")
	response.write("<tr>")
	response.write("<td>")
	
	#Legenda
	response.write("<p class=\"legend\">Legenda pandolerotov<p/>")
	pans = Pandoleros.objects.all()
	for pan in pans:
		response.write("<p class=\"legendPan\">"+str(pan.idP)+": "+pan.name+"<p/>")
	response.write("</td>")
	response.write("</tr>")
	response.write("</table>")
	response.write("</body>") 
	
	
	#GENERIRANJE HTML/KONEC
	return response





def team(request, sType=2,daterange="1970-01-01_00:00:00;2020-01-01_00:00:00"):
	from django.db import models
	
	sTypeDesc = ["tekma","trening", None]
	team = Team(sTypeDesc[int(sType)],daterange=daterange)
	response=HttpResponse("")
	response.write("<head>")
	response.write(style.getStyle())
	response.write("<link href=\"screen\" rel=\"stylesheet\" type=\"text/css\"></link>")
	response.write("</head>")
	response.write("<p class=\"panName\">Mazza la Panda</p>")
	
	if sType == "2":
		sTypeDesc2="Splosno"
	else:
		sTypeDesc2 =sTypeDesc[int(sType)]	

	par = sTypeDesc[int(sType)]

	response.write("<p id=\"stype\">("+str(sTypeDesc2)+")</p>")
	response.write("<body bgcolor=\"bfbfbf\"")
	
	response.write("<table border=\"0\">")
	response.write("<tr><td><p class=\"section\">Napad</p></td></tr>")
	response.write("<tr><td><p class=\"graphTitle\">Stevilo tock</p>")
	response.write("<p class=\"stats\">Povprecno stevilo tock na turn: <b>"+str(round(team.scoreAverage(),2))+"</b></p>")
	response.write("<p class=\"stats\">Povprecno stevilo tock na giron (3*turn): <b>"+str(round(team.scoreAverage()*3,2))+"</b></p>")
	response.write("<p class=\"stats\">Povprecno stevilo tock na tekmo: (2*giron): <b>"+str(round(team.scoreAverage()*6,2))+"</b></p>")
	response.write("<p class=\"stats\">Turni z uspesno servo in branjenjem baze: <b>"+str(round(team.scoreChance()*100,2))+" %</b></p>")
	response.write("<p class=\"stats\">Turni, ki prinesejo tocke : <b>"+str(round(team.scorePositive()*100,2))+" %</b></p>")
	response.write("</td></tr>")
	#response.write("<tr><td><img width=\"700\" src=\"http://"+host+"/past/teamgraph/100/"+str(sType)+"\"></img></td>")
	response.write("<tr><td><img width=\"700\" src=\"http://"+host+"/past/teamgraph/100/"+str(sType)+"/"+daterange+"\"></img></td>")
	response.write("</tr>")
	

	
	response.write("<tr><td><p class=\"graphTitle\">Razlogi za 0 tock</p></td></tr>")
	response.write("<tr><td><img width=\"700\" src=\"http://"+host+"/past/teamgraph/101/"+str(sType)+"/"+daterange+"\"></img></td>")
	response.write("</tr>")
	
	response.write("</table>")
	
	
	response.write("<tr><td><p class=\"section\">Obramba</p></td></tr>")
	response.write("<p class=\"stats\">Ulovljenih serv: <b>"+str(team.caughtServCount())+"</b></p>")
	response.write("<p class=\"stats\">Ulovljenih udarcev: <b>"+str(team.caughtHitCount())+"</b></p>")
	response.write("<p class=\"stats\">Izbitih baz: <b>"+str(team.baseHitCount())+"</b></p>")
	response.write("<p class=\"stats\">Izbitih baz/uspesno serviranje: <b>"+str(round(team.baseHitServRatio()*100,2))+" %</b></p>")
	response.write("<p class=\"stats\">Odbitih pandolov/poskus izbijanja: <b>"+str(round(team.baseDefServRatio()*100,2))+" %</b></p>")
	
	
	#p = Pandoleros.objects.filter(idP=12)[0]
	#p.getTurns(par)
	response.write("<tr><td><p class=\"section\">Pandolerosi</p></td></tr>")
	
	for p in Pandoleros.objects.all():
		if p.idP == ENEMY_ID:
			continue
		p.getTurns(par,daterange)
		response.write("<tr><td><p class=\"graphTitle\">"+p.name+"</p></td></tr>")
		response.write("<p class=\"stats\">Odigranih turnov: <b>"+str(len(p.t))+"</b></p>")
		response.write("<p class=\"stats\">Povprecno st. tock na turn: <b>"+str(round(p.scoreAverage(),2))+"</b></p>")
		response.write("<p class=\"stats\">Povprecno st. FC na turn: <b>"+str(round(p.fcAverage(),2))+"</b></p>")
		response.write("<p class=\"stats\">Linearna aproksimacija forme: <b>"+str(round(p.formLinearFunction()[0],2)) +"x+"+ str(round(p.formLinearFunction()[1],2))+"</b></p>")
		response.write("<p class=\"stats\">Korelacija (joola): <b>"+str(round(p.dopingCC('joola'),2))+"</b></p>")
		response.write("<p class=\"stats\">Korelacija (spric): <b>"+str(round(p.dopingCC('spric'),2))+"</b></p>")
		response.write("<p class=\"stats\">Turnov, ko je uspel servirati: <b>"+str(round(100*p.servTurn(),2))+" %</b></p>")
		response.write("<p class=\"stats\">Ulovljenih: <b>"+str(p.caughtPanCount())+"</b></p>")
		response.write("<p class=\"stats\">Izbitih baz: <b>"+str(p.baseHitCount())+"</b></p>")
		#response.write("<p class=\"stats\">Ulovljenih (od drugih): <b>"+str(p.caughtPanCountOthers())+"</b></p>")
		#response.write("<p class=\"stats\">Izbitih baz (od drugih): <b>"+str(p.baseHitCountOthers())+"</b></p>")
	
	p=Pandoleros.objects.filter(idP=ENEMY_ID)[0]
	p.getTurns();	
	response.write("<tr><td><p class=\"section\">Nasprotniki</p></td></tr>")
	response.write("<p class=\"stats\">Odigranih turnov: <b>"+str(len(p.t))+"</b></p>")
	response.write("<p class=\"stats\">Povprecno st. tock na turn: <b>"+str(round(p.scoreAverage(),2))+"</b></p>")
	response.write("<p class=\"stats\">Povprecno st. FC na turn: <b>"+str(round(p.fcAverage(),2))+"</b></p>")
	response.write("<p class=\"stats\">Linearna aproksimacija forme: <b>"+str(round(p.formLinearFunction()[0],2)) +"x+"+ str(round(p.formLinearFunction()[1],2))+"</b></p>")
	response.write("<p class=\"stats\">Turnov, ko je uspel servirati: <b>"+str(round(100*p.servTurn(),2))+" %</b></p>")
	response.write("<p class=\"stats\">Ulovljenih: <b>"+str(p.caughtPanCount())+"</b></p>")
	response.write("<p class=\"stats\">Izbitih baz: <b>"+str(p.baseHitCount())+"</b></p>")
	response.write("<p class=\"stats\">Ulovljenih (od nas): <b>"+str(p.caughtPanCountOthers())+"</b></p>")
	response.write("<p class=\"stats\">Izbitih baz (od nas): <b>"+str(p.baseHitCountOthers())+"</b></p>")
	return response



def team_select(request, sType=2, testlist="",daterange="1970-01-01_00:00:00;2020-01-01_00:00:00"):
	from django.db import models
	
	sTypeDesc = ["tekma","trening", None]
	selectionList = [int(i) for i in testlist.split(',')]
	team = Team(sTypeDesc[int(sType)],selection=True, list=selectionList,daterange=daterange)
	response=HttpResponse("")
	response.write("<head>")
	response.write(style.getStyle())
	response.write("<link href=\"screen\" rel=\"stylesheet\" type=\"text/css\"></link>")
	response.write("</head>")
	response.write("<p class=\"panName\">Selekcija</p>")
	
	if sType == "2":
		sTypeDesc2="Splosno"
	else:
		sTypeDesc2 =sTypeDesc[int(sType)]	

	par = sTypeDesc[int(sType)]

	response.write("<p id=\"stype\">("+str(sTypeDesc2)+")</p>")
	response.write("<body bgcolor=\"bfbfbf\"")
	
	response.write("<table border=\"0\">")
	response.write("<tr><td><p class=\"section\">Napad</p></td></tr>")
	response.write("<tr><td><p class=\"graphTitle\">Stevilo tock</p>")
	response.write("<p class=\"stats\">Povprecno stevilo tock na turn: <b>"+str(round(team.scoreAverage(),2))+"</b></p>")
	response.write("<p class=\"stats\">Povprecno stevilo tock na giron (3*turn): <b>"+str(round(team.scoreAverage()*3,2))+"</b></p>")
	response.write("<p class=\"stats\">Povprecno stevilo tock na tekmo: (2*giron): <b>"+str(round(team.scoreAverage()*6,2))+"</b></p>")
	response.write("<p class=\"stats\">Turni z uspesno servo in branjenjem baze: <b>"+str(round(team.scoreChance()*100,2))+" %</b></p>")
	response.write("<p class=\"stats\">Turni, ki prinesejo tocke : <b>"+str(round(team.scorePositive()*100,2))+" %</b></p>")
	response.write("</td></tr>")
	response.write("<tr><td><img width=\"700\" src=\"http://"+host+"/past/team_select_graph/100/"+testlist+"/"+str(sType)+"/"+daterange+"\"></img></td>")
	response.write("</tr>")
	

	
	response.write("<tr><td><p class=\"graphTitle\">Razlogi za 0 tock</p></td></tr>")
	response.write("<tr><td><img width=\"700\" src=\"http://"+host+"/past/team_select_graph/101/"+testlist+"/"+str(sType)+"/"+daterange+"\"></img></td>")
	#response.write("<tr><td><img width=\"700\" src=\"http://"+host+"/past/teamgraph/101/"+str(sType)+"\"></img></td>")
	response.write("</tr>")
	
	response.write("</table>")
	
	
	response.write("<tr><td><p class=\"section\">Obramba</p></td></tr>")
	response.write("<p class=\"stats\">Ulovljenih serv: <b>"+str(team.caughtServCount())+"</b></p>")
	response.write("<p class=\"stats\">Ulovljenih udarcev: <b>"+str(team.caughtHitCount())+"</b></p>")
	response.write("<p class=\"stats\">Izbitih baz: <b>"+str(team.baseHitCount())+"</b></p>")
	response.write("<p class=\"stats\">Izbitih baz/uspesno serviranje: <b>"+str(round(team.baseHitServRatio()*100,2))+" %</b></p>")
	response.write("<p class=\"stats\">Odbitih pandolov/poskus izbijanja: <b>"+str(round(team.baseDefServRatio()*100,2))+" %</b></p>")
	
	
	#p = Pandoleros.objects.filter(idP=12)[0]
	#p.getTurns(par)
	response.write("<tr><td><p class=\"section\">Pandolerosi</p></td></tr>")
	
	for p in Pandoleros.objects.all():
		if p.idP not in selectionList:
			continue
		p.getTurns(par,daterange)
		response.write("<tr><td><p class=\"graphTitle\">"+p.name+"</p></td></tr>")
		response.write("<p class=\"stats\">Odigranih turnov: <b>"+str(len(p.t))+"</b></p>")
		response.write("<p class=\"stats\">Povprecno st. tock na turn: <b>"+str(round(p.scoreAverage(),2))+"</b></p>")
		response.write("<p class=\"stats\">Povprecno st. FC na turn: <b>"+str(round(p.fcAverage(),2))+"</b></p>")
		response.write("<p class=\"stats\">Linearna aproksimacija forme: <b>"+str(round(p.formLinearFunction()[0],2)) +"x+"+ str(round(p.formLinearFunction()[1],2))+"</b></p>")
		response.write("<p class=\"stats\">Korelacija (joola): <b>"+str(round(p.dopingCC('joola'),2))+"</b></p>")
		response.write("<p class=\"stats\">Korelacija (spric): <b>"+str(round(p.dopingCC('spric'),2))+"</b></p>")
		response.write("<p class=\"stats\">Turnov, ko je uspel servirati: <b>"+str(round(100*p.servTurn(),2))+" %</b></p>")
		response.write("<p class=\"stats\">Ulovljenih: <b>"+str(p.caughtPanCount())+"</b></p>")
		response.write("<p class=\"stats\">Izbitih baz: <b>"+str(p.baseHitCount())+"</b></p>")
		#response.write("<p class=\"stats\">Ulovljenih (od drugih): <b>"+str(p.caughtPanCountOthers())+"</b></p>")
		#response.write("<p class=\"stats\">Izbitih baz (od drugih): <b>"+str(p.baseHitCountOthers())+"</b></p>")
	

	return response




#######################
#LEGENDA GRAFOV (num) #
#######################
#0	TOCKE NA TURN
#1	FC NA TURN
#2	KORELACIJSKI KOEFICIENT (TOCKE/JOOLA)
#3	KORELACIJSKI KOEFICIENT (TOCKE/SPRIC)
#4	KORELACIJSKI KOEFICIENT (TOCKE/JOOLA+SPRIC)
#5	PANDOLE SO MU ULOVILI
#6	PANDOLE JE ULOVIL
#7	BAZO SO MU IZBILI
#8	BAZO JE IZBIL
#9	PORAZDELITEV USPESNIH SERV
#10	RAZLOGI ZA NEUSPESNE SERVE
#11	POSLEDICE UDARCEV
#12	RAZLOGI ZA NEUSPESNE UDARCE
#100 EKIPA: TOCKE NA TURN


def graph(request, id, num,sType,daterange="1970-01-01_00:00:00;2020-01-01_00:00:00"):
	'''Narise zahtevan graf, podan z <num>, za igralca <id>, za cas <daterange>'''
	import random
	import datetime
	from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
	from matplotlib.figure import Figure
	from matplotlib.dates import DateFormatter


	fig=Figure()
	ax=fig.add_subplot(111)
	num = int(num)
	x=[]
	y=[]

	parString  = ["tekma","trening",None]

	if num>=100: 
		team = Team(parString[int(sType)],daterange=daterange)
		
	else:
		p = Pandoleros.objects.filter(idP=int(id))[0]
		p.getTurns(parString[int(sType)],daterange)
	

	# --DEPRECATED
	if int(sType) < 2:
		t = Turns.objects.filter(idP=id, turn_type=int(sType))
	else:
		t = Turns.objects.filter(idP=id)

	dates=parseDates(daterange)
	t = [turn for turn in t if turn.date >= dates[0] and turn.date <= dates[1]]


#0	TOCKE NA TURN
	if int(num) == 0:
		x = range(len(t))
		y = p.scoreVector();
		ax.plot(x, y, '-')
		
		t=p.fcVector();
		ax.plot(x, t[0], 'go')
		ax.plot(x, t[1], 'yd')
		ax.plot(x, t[2], 'rs')

		a = p.scoreAverage()
		y = [a for i in x]
		ax.plot(x,y,'r')

#1	FC NA TURN --DEPRECATED
	elif int(num) == 1:
		x = range(len(t))
		y = [(not i.hit1_id) + (not i.hit2_id) + (not i.hit3_id) for i in t]
		ax.plot(x, y, 'go')
		

#2	KORELACIJSKI KOEFICIENT (TOCKE/JOOLA)
	elif int(num) == 2:

		xy = p.dopingVector(1,0)
		ax.bar(xy[0],xy[1], color='green', width=0.5)
		ax.set_xlabel("Joole")
		ax.set_ylabel("Povprecen koncni rezultat")

#3	KORELACIJSKI KOEFICIENT (TOCKE/SPRIC)
	elif int(num) == 3:

		xy = p.dopingVector(0,1)
		ax.bar(xy[0],xy[1], color='yellow', width=0.5)
		ax.set_xlabel("Sprici")
		ax.set_ylabel("Povprecen koncni rezultat")

#4	KORELACIJSKI KOEFICIENT (TOCKE/JOOLA+SPRIC)
	elif int(num) == 4:
		
		xy = p.dopingVector(1,1)
		ax.bar(xy[0],xy[1], color='red', width=0.5)
		ax.set_xlabel("Joole+Sprici")
		ax.set_ylabel("Povprecen koncni rezultat")


#5	PANDOLE SO MU ULOVILI
	elif int(num) == 5:

		xy=p.caughtPanCountOthersVector();
		ax.bar(xy[0],xy[1], color='red', width=0.5)
		ax.set_xlabel("Pandolerosi")
		ax.set_ylabel("Ulovljeni pandoli")


#6	PANDOLE JE ULOVIL
	elif int(num) == 6:

		xy=p.caughtPanCountVector();
		ax.bar(xy[0],xy[1], color='yellow', width=0.5)
		ax.set_xlabel("Pandolerosi")
		ax.set_ylabel("Ulovljeni pandoli")


#7	BAZO SO MU IZBILI
	elif int(num) == 7:

		xy = p.baseHitCountOthersVector();
		ax.bar(xy[0],xy[1], color='red', width=0.5)
		ax.set_xlabel("Pandolerosi")
		ax.set_ylabel("Izbite baze")


#8	BAZO JE IZBIL
	elif int(num) == 8:

		xy = p.baseHitCountVector();
		ax.bar(xy[0],xy[1], color='yellow', width=0.5)
		ax.set_xlabel("Pandolerosi")
		ax.set_ylabel("Izbite baze")


	
#9	PORAZDELITEV USPESNIH SERV
	elif int(num) == 9:
		
		s =p.servDistributionVector();
		ax.pie(s, labels=["1.","2.","3."], shadow=True)


#10	RAZLOGI ZA NEUSPESNE SERVE
	elif int(num) == 10:
		
		s=p.servReasonVector();
		ax.pie(s, labels=["Out","Zgresen","Ujet"], shadow=True)	


#11	POSLEDICE UDARCEV
	elif int(num) == 11:
		
		s=p.hitEventVector();
		ax.pie(s, labels=["FC","+12-FC","-12","Ni dvignil","Zgresen","Ujet"], shadow=True)

#12	RAZLOGI ZA NEUSPESNE UDARCE
	elif int(num) == 12:
		
		s=p.hitReasonVector();
		ax.pie(s, labels=["Ni dvignil","Zgresen","Ujet"], shadow=True)
		


#13	LINEARNA APROKSIMACIJA FORMA n=1
	elif int(num) == 13:
	
		
		y=p.scoreVector()
		x=range(len(y))
		ax.plot(x,y,'gx')
	
		xy=p.formLinearVector(1)
		ax.plot(xy[0],xy[1],'y')
		
		#xy=p.formLinearVector(3)
		#ax.plot(xy[0],xy[1],'g')
		
		xy=p.formLinearVector(3)
		ax.plot(xy[0],xy[1],'r')

	else:
		pass

	canvas=FigureCanvas(fig)
	response=HttpResponse(content_type='image/png')
	canvas.print_png(response)
	return response

def teamGraph(request,num,sType,testlist="",daterange="1970-01-01_00:00:00;2020-01-01_00:00:00"):
	'''Narise zahtevan graf, podan z <num>, za igralca <id>'''
	import random
	import datetime
	from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
	from matplotlib.figure import Figure
	from matplotlib.dates import DateFormatter


	fig=Figure()
	ax=fig.add_subplot(111)



	sTypeDesc  = ["tekma","trening",None]

	if len(testlist) > 0:
		selectionList = [int(i) for i in testlist.split(',')]
		team = Team(sTypeDesc[int(sType)], True, selectionList,daterange=daterange)
	else:
		team = Team(sTypeDesc[int(sType)],daterange=daterange)

#100 EKIPA: TOCKE NA TURN
	if int(num) == 100:
		scVector=team.scoreVector()
		x = range(len(scVector))
		ax.plot(x,scVector)
		
		a = sum(scVector)/len(scVector)
		y = [a for i in x]
		ax.plot(x,y,'r')
		
#101 EKIPA: RAZLOG ZA 0 TOCK
	if int(num) == 101:
		szVector = team.scoreZeroReason()
		ln = float(sum(szVector))
		l = ["Neuspesne\n serve \n"+str(round(szVector[0]/ln*100,2))+"%","Izbita baza\n"+str(round(szVector[1]/ln*100,2))+"%","Ulovljen pandolo\n"+str(round(szVector[2]/ln*100,2))+"%","Neuspesni udarci\n"+str(round(szVector[3]/ln*100,2))+"%"]
		ax.pie(szVector, labels=l, shadow=True)


	canvas=FigureCanvas(fig)
	response=HttpResponse(content_type='image/png')
	canvas.print_png(response)
	return response
